package vehicles;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ConcurrentVehicleTracker {

	private final ConcurrentMap<String, Point> locations;
	private final Map<String, Point> unmodifiableMap;
	
	public ConcurrentVehicleTracker(Map<String, Point> data) {
		locations = new ConcurrentHashMap<>(data);
		unmodifiableMap = Collections.unmodifiableMap(locations);
	}

	private Point safeGet(String name) {
		Point loc = locations.get(name);
		if (loc == null) throw new IllegalArgumentException("Illegal name");
		return loc;
	}
	
	public Point getLocation(String name) {
		return safeGet(name);
	}
	
	public void setLocation(String name, Point point) {		
		if (locations.replace(name, new ImmutablePoint(point.getX(), point.getY())) == null)
			throw new IllegalArgumentException("Illegal name");
	}
	
	public Map<String, Point> getLocations() {
		return unmodifiableMap;
	}
	
	public Map<String, Point> getLocationsAsSnapshot() {
		return Collections.unmodifiableMap(new HashMap<String, Point>(locations));
	}
}
